package com.bena.udvtui2;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.widget.*;


public class InfoPage extends Activity{
	ImageButton backButton;
	ImageButton pauseButton;
	ImageButton restartButton;
	TextView textBox;
	String[] cs;
	int seenLocs;
	String newString;
	MediaRecorder audioListener;
	MediaPlayer player;
	String pathForAppFiles;

	@Override  // override the back button to call finish
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			Log.d(this.getClass().getName(), "back button pressed");
			onPause();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		Log.d("INFO","Start");
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
		setContentView(R.layout.showinfo);
		SQLiteDatabase myDatabase;
		myDatabase = openOrCreateDatabase( "/mnt/sdcard/UDVT/UDVirtualTour.db", SQLiteDatabase.CREATE_IF_NECESSARY, null); 
		backButton = (ImageButton) findViewById(R.id.infoButton);
		textBox = (TextView) findViewById(R.id.infoText);
		Log.d("INFO","DATA TRANSFER");
		/* DATA TRANSFER START */
		Intent myIntent = getIntent();  // get the intent sent to the Activity
		Bundle bundle = new Bundle();
		//final String newString;
		Log.d("INFO","loading bundle");
		bundle=myIntent.getBundleExtra("bundle");  // get the bundle from the intent
		Log.d("INFO","done with bundle");
		cs=bundle.getStringArray("seen");
		seenLocs=bundle.getInt("count");
		Log.d("INFO","for loop start  "+seenLocs);
		for(int i=0; i<seenLocs;i++){  // print everything in the array
			Log.d("INFO_PAGE_SEEN",cs[i]);
		}
		Log.d("INFO","loop complete");
		/* DATA TRANSFER COMPLETE */
		Log.d("INFO","data transfer stop ");

		ImageView picture = (ImageView) findViewById(R.id.infoImage);
		if(cs != null){
			Log.d("SQL","entered");
			newString=cs[seenLocs-1];
			String Selection = "filename = \""+ newString +"\"";
			//String SelectionArgs[] = {newString};
			Cursor c = myDatabase.query("UDVirtualTour", null, Selection, null, null, null, null);
			c.moveToFirst();
			String fullText = c.getString(4);

			textBox.setText(fullText);
			Log.e("STRING", fullText);
			//Log.e("STRING", c.getString(4).substring(182));
			Bitmap image = BitmapFactory.decodeFile(c.getString(5));// = new Bitmap();
			picture.setImageBitmap(image);
			/*Log.d("SQL TEST",c.getString(0));
            Log.d("SQL TEST",c.getString(1));
            Log.d("SQL TEST",c.getString(2));
            Log.d("SQL TEST",c.getString(3));
            Log.d("SQL TEST",c.getString(4));
            Log.d("SQL TEST",c.getString(5));
            Log.d("SQL TEST",c.getString(6));*/


			if (player!=null)
				player.release();
			if (player == null) {
				player = new MediaPlayer ();
			}
			String audioFilePath = c.getString(6);
			Log.d("AUDIO",audioFilePath );

			try {
				player.setDataSource(audioFilePath);
				player.prepare();
				player.start();
			} catch (Exception e) {
				Log.e("Audio", "Playback failed.", e);
			}
			Log.d("AUDIO","play passed" );
		}
		else
			textBox.setText("Error in selection.");
		//Toast.makeText(InfoPage.this, "info: " + newString, Toast.LENGTH_SHORT).show(); 

		backButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putStringArray("seen",cs);
				bundle.putInt("count", seenLocs);
				Intent resultIntent = new Intent();
				resultIntent.putExtras(bundle);
				setResult(Activity.RESULT_OK, resultIntent);
				finish();

			}
		});

		restartButton = (ImageButton) findViewById(R.id.restartButton);
		pauseButton = (ImageButton) findViewById(R.id.pauseStartButton);

		restartButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				player.stop();
				try {
					//player.setDataSource(audioFilePath);
					player.prepare();
					player.start();
				} catch (Exception e) {
					Log.e("Audio", "Playback failed.", e);
				}
				Log.d("AUDIO","play passed" );
			}
		});
		pauseButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				//player.stop();
				if(player.isPlaying()){
					player.pause();
				}
				else{
					player.start();
				}
			}
		});

	}

	protected void onPause(){
		super.onPause();
		player.stop();
		Bundle bundle = new Bundle();
		bundle.putStringArray("seen",cs);
		bundle.putInt("count", seenLocs);
		Intent resultIntent = new Intent();
		resultIntent.putExtras(bundle);
		setResult(Activity.RESULT_OK, resultIntent);
		finish();
	}
}
